#include "interface_loader.h"
/*--------------------------------------------------------------------------------*/
#include "line_parser.h"
#include "input_file_manager.h"
#include "debug.h"
#include "interface_text_button.h"
/*--------------------------------------------------------------------------------*/
INTERFACE_CONTAINER::REF INTERFACE_LOADER::LoadContainerFromFile( const TEXT & file_name )
{
	INTERFACE_CONTAINER::REF
		result;
	LINE_PARSER
		declaration_parser( "*:*" ),
		property_parser( "*=*" );
	INPUT_FILE
		& file = INPUT_FILE_MANAGER_GetFile( file_name );
	TEXT
		line;
	STATE
		state = STATE_ObjectDeclaration;
	INTERFACE_OBJECT::REF
		current_object;
	PROPERTY_TABLE
		current_property_table;

	result = new INTERFACE_CONTAINER;

	file.Open();

	while( file.ReadLine( line ) )
	{
		if( state == STATE_ObjectDeclaration )
		{
			if( declaration_parser.Parse( line ) )
			{
				TEXT
					type = declaration_parser.GetText( 0 ),
					name = declaration_parser.GetText( 1 );

				if( type == "text_button" )
				{
					current_object = new INTERFACE_TEXT_BUTTON;
					state = STATE_OpenBracket;
				}
				else
				{
					DEBUG_LogLine( "INTERFACE_LOADER: Unknown element :" + type );
				}
			}
		}
		else if( state == STATE_OpenBracket )
		{
			if( line[ 0 ] == '{' )
			{
				state = STATE_ObjectDescription;
			}
		}
		else if( state == STATE_ObjectDescription )
		{
			if( property_parser.Parse( line ) )
			{
				current_property_table.Add( property_parser.GetText( 0 ), property_parser.GetText( 1 ) );
			}
			else if( line[ 0 ] == '}' )
			{
				current_object->Initialize( current_property_table );
				result->Add( current_object );
				current_property_table.Clear();
				state = STATE_ObjectDeclaration;
			}
		}
	}

	return result;
}
/*--------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------*/

